{ "cells": [ { "cell_type": "markdown", "id": "0af62e9f", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Big Number Conversion" ] }, { "cell_type": "markdown", "id": "1d780ee8", "metadata": { "slideshow": { "slide_type": "-" }, "tags": [ "remove-cell" ] }, "source": [ "**CS1302 Introduction to Computer Programming**\n", "___" ] }, { "cell_type": "code", "execution_count": 1, "id": "5c484b36", "metadata": {}, "outputs": [], "source": [ "from math import floor, log2" ] }, { "cell_type": "markdown", "id": "45363c7e", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Conversion to Decimal" ] }, { "cell_type": "markdown", "id": "2d0b6cc0", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "In this notebook, we will use iterations to convert numbers with arbitrary size." ] }, { "cell_type": "markdown", "id": "dda6a986", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Binary-to-Decimal" ] }, { "cell_type": "markdown", "id": "f5d167f3", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "In a previous lab, we considered converting a byte string to decimal. What about converting a binary string of arbitrary length to decimal?" ] }, { "cell_type": "markdown", "id": "18784aaa", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "````{prf:definition} Binary numbers\n", "\n", "Given a binary string of an arbitrarily length $k$,\n", "\n", "$$ \n", "b_{k-1}\\circ \\dots \\circ b_1\\circ b_0,\n", "$$\n", "the decimal number is given by the formula\n", "\n", "$$\n", "2^0 \\cdot b_0 + 2^1 \\cdot b_1 + \\dots + 2^{k-1} \\cdot b_{k-1}.\n", "$$ (b2d:1)\n", "\n", "````" ] }, { "cell_type": "markdown", "id": "1a4eaba1", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "In mathematics, we use the summation notation to write the above formula {eq}`b2d:1`:\n", "\n", "$$ \n", "\\sum_{i=0}^{k-1} 2^i \\cdot b_{i}.\n", "$$ (b2d:2)" ] }, { "cell_type": "markdown", "id": "524a2f4a", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "In a program, the formula can be implemented as a for loop:" ] }, { "cell_type": "code", "execution_count": 2, "id": "27dabb4b", "metadata": {}, "outputs": [], "source": [ "def binary_to_decimal_v1(binary_str):\n", " k = len(binary_str)\n", " decimal = 0 # initialization\n", " for i in range(k):\n", " decimal += 2 ** i * int(binary_str[(k - 1) - i]) # iteration\n", " return decimal" ] }, { "cell_type": "markdown", "id": "c631fe61", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Note that $b_i$ is given by `binary_str[(k-1)-i]` for different index `i` as shown below:\n", "\n", "$$\n", "\\begin{array}{c|c:c:c:c|}\\texttt{binary_str} & b_{k-1} & b_{k-2} & \\dots & b_0\\\\ \\text{indexing} & [0] & [1] & \\dots & [k-1] \\end{array}\n", "$$" ] }, { "cell_type": "markdown", "id": "60fccfa3", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "The following is another way to write the for loop." ] }, { "cell_type": "code", "execution_count": 3, "id": "b83dc8f1", "metadata": {}, "outputs": [], "source": [ "def binary_to_decimal_v2(binary_str):\n", " decimal = 0 # initialization\n", " for bit in binary_str:\n", " decimal = decimal * 2 + int(bit) # iteration\n", " return decimal" ] }, { "cell_type": "markdown", "id": "0b0c44c6", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "The algorithm implements the same formula factorized as follows:\n", "\n", "$$\n", "\\begin{aligned} \\sum_{i=0}^{k-1} 2^i \\cdot b_{i} \n", "&= \\left(\\sum_{i=1}^{k-1} 2^i \\cdot b_{i}\\right) + b_0\\\\\n", "&= \\left(\\sum_{i=1}^{k-1} 2^{i-1} \\cdot b_{i}\\right)\\times 2 + b_0 \\\\\n", "&= \\left(\\sum_{j=0}^{k-2} 2^{j} \\cdot b_{j+1}\\right)\\times 2 + b_0 && \\text{with $j=i-1$} \\\\\n", "&= \\underbrace{(\\dots (\\underbrace{(\\underbrace{\\overbrace{0}^{\\text{initialization}\\kern-2em}\\times 2 + b_{k-1}}_{\\text{first iteration} }) \\times 2 + b_{k-2}}_{\\text{second iteration} }) \\dots )\\times 2 + b_0}_{\\text{last iteration} }.\\end{aligned}\n", "$$ (b2d:3)" ] }, { "cell_type": "markdown", "id": "9f15d242", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**Exercise** Write your own converter `binary_to_decimal` below. Make it as efficient as possible." ] }, { "cell_type": "code", "execution_count": 4, "id": "b73572eb", "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "f1e194aff055633aead3f89c8047a17a", "grade": false, "grade_id": "binary_to_decimal", "locked": false, "schema_version": 3, "solution": true, "task": false }, "slideshow": { "slide_type": "-" }, "tags": [ "remove-output" ] }, "outputs": [], "source": [ "def binary_to_decimal(binary_str):\n", " # YOUR CODE HERE\n", " raise NotImplementedError()\n", " return decimal" ] }, { "cell_type": "markdown", "id": "29e03709", "metadata": {}, "source": [ "````{hint}\n", "\n", "You can choose one of the two implementations above but take the time to type in the code instead of copy-and-paste.\n", "\n", "````" ] }, { "cell_type": "code", "execution_count": 5, "id": "6c916325", "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "91fc75df3ab7d19f958f9c774d809914", "grade": true, "grade_id": "test-binary_to_decimal", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false }, "slideshow": { "slide_type": "-" }, "tags": [ "hide-input", "remove-output" ] }, "outputs": [ { "ename": "NotImplementedError", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNotImplementedError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0mtest_binary_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"0\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 14\u001b[0m \u001b[0mtest_binary_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m255\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"11111111\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0mtest_binary_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m52154\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"1100101110111010\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mtest_binary_to_decimal\u001b[0;34m(decimal, binary_str)\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mtest_binary_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdecimal\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbinary_str\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mdecimal_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbinary_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbinary_str\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0mcorrect\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdecimal_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mdecimal_\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mdecimal\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mcorrect\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mbinary_to_decimal\u001b[0;34m(binary_str)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mbinary_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbinary_str\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# YOUR CODE HERE\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mNotImplementedError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mdecimal\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNotImplementedError\u001b[0m: " ] } ], "source": [ "# tests\n", "import numpy as np\n", "\n", "\n", "def test_binary_to_decimal(decimal, binary_str):\n", " decimal_ = binary_to_decimal(binary_str)\n", " correct = isinstance(decimal_, int) and decimal_ == decimal\n", " if not correct:\n", " print(f\"{binary_str} should give {decimal} not {decimal_}.\")\n", " assert correct\n", "\n", "\n", "test_binary_to_decimal(0, \"0\")\n", "test_binary_to_decimal(255, \"11111111\")\n", "test_binary_to_decimal(52154, \"1100101110111010\")\n", "test_binary_to_decimal(3430, \"110101100110\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "30af2bd1", "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "4021762033790ce89b1b10c76cbe7dae", "grade": true, "grade_id": "hidden_test-binary_to_decimal", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false }, "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "# hidden tests" ] }, { "cell_type": "code", "execution_count": 7, "id": "1ca89ec3", "metadata": { "code_folding": [ 0 ], "slideshow": { "slide_type": "-" }, "tags": [ "remove-output" ] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "aefb1e67616b4fc485e083336432f8d0", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Text(value='1011', description='binary_str'), Output()), _dom_classes=('widget-interact'…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# binary-to-decimal converter\n", "from ipywidgets import interact\n", "\n", "bits = [\"0\", \"1\"]\n", "\n", "\n", "@interact(binary_str=\"1011\")\n", "def convert_byte_to_decimal(binary_str):\n", " for bit in binary_str:\n", " if bit not in bits:\n", " print(\"Not a binary string.\")\n", " break\n", " else:\n", " print(\"decimal:\", binary_to_decimal(binary_str))" ] }, { "cell_type": "markdown", "id": "f384b93e", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Undecimal-to-Decimal" ] }, { "cell_type": "markdown", "id": "ffd423fb", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "A base-11 number system is called an [undecimal system](https://en.wikipedia.org/wiki/Undecimal). The digits range from 0 to 10 with 10 denoted as X:\n", "\n", "$$\n", "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, X.\n", "$$\n", "\n", "The [International Standard Book Number (ISBN)](https://en.wikipedia.org/wiki/International_Standard_Book_Number) uses an undecimal digit." ] }, { "cell_type": "markdown", "id": "11c7c742", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**Exercise** In the following code, assign to `decimal` the integer represented by an undecimal string of arbitrary length." ] }, { "cell_type": "markdown", "id": "fd85e0dc", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "````{hint}\n", "\n", "Write a conditional to \n", "1. check if a digit is (capital) `'X'`, and if so, \n", "2. convert the digit to the integer value 10.\n", "\n", "````" ] }, { "cell_type": "code", "execution_count": 8, "id": "2f67b38f", "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "55bbfbe8143ad30f684efd927989183e", "grade": false, "grade_id": "undecimal_to_decimal", "locked": false, "schema_version": 3, "solution": true, "task": false }, "slideshow": { "slide_type": "subslide" }, "tags": [ "remove-output" ] }, "outputs": [], "source": [ "def undecimal_to_decimal(undecimal_str):\n", " # YOUR CODE HERE\n", " raise NotImplementedError()\n", " return decimal" ] }, { "cell_type": "code", "execution_count": 9, "id": "acf91182", "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "af19b56f1f1c98807e67e6ab8829606a", "grade": true, "grade_id": "test-undecimal_to_decimal", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false }, "slideshow": { "slide_type": "-" }, "tags": [ "hide-input", "remove-output" ] }, "outputs": [ { "ename": "NotImplementedError", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNotImplementedError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0mtest_undecimal_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m27558279079916281\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"6662X0X584839464\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 11\u001b[0m \u001b[0mtest_undecimal_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m23022771839270\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"73769X2556695\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0mtest_undecimal_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m161804347284488\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"476129248X2067\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mtest_undecimal_to_decimal\u001b[0;34m(decimal, undecimal_str)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# tests\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mtest_undecimal_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdecimal\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mundecimal_str\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mdecimal_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mundecimal_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mundecimal_str\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mcorrect\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdecimal_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mdecimal_\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mdecimal\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mcorrect\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mundecimal_to_decimal\u001b[0;34m(undecimal_str)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mundecimal_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mundecimal_str\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# YOUR CODE HERE\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mNotImplementedError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mdecimal\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNotImplementedError\u001b[0m: " ] } ], "source": [ "# tests\n", "def test_undecimal_to_decimal(decimal, undecimal_str):\n", " decimal_ = undecimal_to_decimal(undecimal_str)\n", " correct = isinstance(decimal_, int) and decimal_ == decimal\n", " if not correct:\n", " print(f\"{undecimal_str} should give {decimal} not {decimal_}.\")\n", " assert correct\n", "\n", "\n", "test_undecimal_to_decimal(27558279079916281, \"6662X0X584839464\")\n", "test_undecimal_to_decimal(23022771839270, \"73769X2556695\")\n", "test_undecimal_to_decimal(161804347284488, \"476129248X2067\")" ] }, { "cell_type": "code", "execution_count": 10, "id": "85c7da77", "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "da072224351c55417d95cad1736e15af", "grade": true, "grade_id": "hidden_test-undecimal_to_decimal", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false }, "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "# hidden tests" ] }, { "cell_type": "code", "execution_count": 11, "id": "d5100bb0", "metadata": { "code_folding": [ 0 ], "slideshow": { "slide_type": "-" }, "tags": [ "remove-output" ] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "1aa044f59e65418d940fa9245d6367c8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Text(value='X', description='undecimal_str'), Output()), _dom_classes=('widget-interact'…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# undecimal-to-decimal calculator\n", "from ipywidgets import interact\n", "\n", "undecimal_digits = [str(i) for i in range(10)] + [\"X\"]\n", "\n", "\n", "@interact(undecimal_str=\"X\")\n", "def convert_undecimal_to_decimal(undecimal_str):\n", " for digit in undecimal_str:\n", " if digit not in undecimal_digits:\n", " print(\"Not an undecimal string.\")\n", " break\n", " else:\n", " print(\"decimal:\", undecimal_to_decimal(undecimal_str))" ] }, { "cell_type": "markdown", "id": "ec27f1aa", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Conversion from Decimal" ] }, { "cell_type": "markdown", "id": "326acb91", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Consider the reverse process that converts a non-negative decimal number of arbitrary size to a string representation in another number system." ] }, { "cell_type": "markdown", "id": "9c1a6e00", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "### Decimal-to-Binary" ] }, { "cell_type": "markdown", "id": "6bc3bb2f", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "The following code converts a decimal number to a binary string." ] }, { "cell_type": "markdown", "id": "0326411c", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "```Python\n", "def decimal_to_binary(decimal):\n", " binary_str = str(decimal % 2)\n", " while decimal // 2:\n", " decimal //= 2\n", " binary_str = str(decimal % 2) + binary_str\n", " return binary_str\n", "```" ] }, { "cell_type": "markdown", "id": "46f51715", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "To understand the while loop, consider the same formula {eq}`b2d:3`, where the braces indicate the value of `decimal` at different times:\n", "\n", "$$\n", "\\begin{aligned} \\sum_{i=0}^{k-1} 2^i \\cdot b_{i} &= \\left(\\sum_{i=0}^{k-2} 2^{i-2} \\cdot b_{i-1}\\right)\\times 2 + b_0 \\\\\n", "&= \\underbrace{(\\underbrace{ \\dots (\\underbrace{(0\\times 2 + b_{k-1}) \\times 2 + b_{k-2}}_{\\text{right before the last iteration} } )\\times 2 \\dots + b_1}_{\\text{right before the second iteration} })\\times 2 + b_0}_{\\text{right before the first iteration} }.\\end{aligned}\n", "$$" ] }, { "cell_type": "markdown", "id": "82b5a053", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- $b_0$ is the remainder `decimal % 2` right before the first iteration,\n", "- $b_1$ is the remainder `decimal // 2 % 2` right before the second iteration, and\n", "- $b_{k-1}$ is the remainder `decimal // 2 % 2` right before the last iteration." ] }, { "cell_type": "markdown", "id": "26c9e05e", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "We can also write a for loop instead of a while loop:" ] }, { "cell_type": "code", "execution_count": 12, "id": "0e1702f9", "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "def decimal_to_binary(decimal):\n", " binary_str = \"\"\n", " num_bits = 1 + (decimal and floor(log2(decimal)))\n", " for i in range(num_bits):\n", " binary_str = str(decimal % 2) + binary_str\n", " decimal //= 2\n", " return binary_str" ] }, { "cell_type": "code", "execution_count": 13, "id": "f4082f66", "metadata": { "code_folding": [ 0 ], "slideshow": { "slide_type": "-" }, "tags": [ "remove-output" ] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "399f895b70ee46948318a49d7f3e8ea7", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Text(value='11', description='decimal'), Output()), _dom_classes=('widget-interact',))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# decimal-to-binary calculator\n", "@interact(decimal=\"11\")\n", "def convert_decimal_to_binary(decimal):\n", " if not decimal.isdigit():\n", " print(\"Not a non-negative integer.\")\n", " else:\n", " print(\"binary:\", decimal_to_binary(int(decimal)))" ] }, { "cell_type": "markdown", "id": "06aaa601", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**Exercise** Explain what the expression `1 + (decimal and floor(log2(decimal)))` calculates. In particular, explain the purpose of the logical `and` operation in the expression?" ] }, { "cell_type": "markdown", "id": "03df0d05", "metadata": { "deletable": false, "nbgrader": { "cell_type": "markdown", "checksum": "12d1969b35591f01fcf2478d720b06aa", "grade": true, "grade_id": "number-of-bits", "locked": false, "points": 1, "schema_version": 3, "solution": true, "task": false }, "slideshow": { "slide_type": "-" } }, "source": [ "YOUR ANSWER HERE" ] }, { "cell_type": "markdown", "id": "2b1a4ee6", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Decimal-to-Undecimal" ] }, { "cell_type": "markdown", "id": "c995cf3f", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**Exercise** Assign to `undecimal_str` the undecimal string that represents a non-negative integer `decimal` of any size." ] }, { "cell_type": "code", "execution_count": 14, "id": "3841bde8", "metadata": { "code_folding": [ 0 ], "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "701b02e9f4258346bf3709536abd9661", "grade": false, "grade_id": "decimal_to_undecimal", "locked": false, "schema_version": 3, "solution": true, "task": false }, "slideshow": { "slide_type": "-" }, "tags": [ "remove-output" ] }, "outputs": [], "source": [ "def decimal_to_undecimal(decimal):\n", " # YOUR CODE HERE\n", " raise NotImplementedError()\n", " return undecimal_str" ] }, { "cell_type": "code", "execution_count": 15, "id": "53fc90c1", "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "af7198d1334cca1f1b3e7cbef25622e4", "grade": true, "grade_id": "test-decimal_to_undecimal", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false }, "slideshow": { "slide_type": "-" }, "tags": [ "hide-input", "remove-output" ] }, "outputs": [ { "ename": "NotImplementedError", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNotImplementedError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0mtest_decimal_to_undecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"X\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0mtest_decimal_to_undecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"0\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0mtest_decimal_to_undecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"1752572309X478\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m57983478668530\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mtest_decimal_to_undecimal\u001b[0;34m(undecimal, decimal)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# tests\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mtest_decimal_to_undecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mundecimal\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdecimal\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mundecimal_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdecimal_to_undecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdecimal\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mcorrect\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mundecimal\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mundecimal\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mundecimal_\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mcorrect\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mdecimal_to_undecimal\u001b[0;34m(decimal)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mdecimal_to_undecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdecimal\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# YOUR CODE HERE\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mNotImplementedError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mundecimal_str\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNotImplementedError\u001b[0m: " ] } ], "source": [ "# tests\n", "def test_decimal_to_undecimal(undecimal, decimal):\n", " undecimal_ = decimal_to_undecimal(decimal)\n", " correct = isinstance(undecimal, str) and undecimal == undecimal_\n", " if not correct:\n", " print(\n", " f\"{decimal} should be represented as the undecimal string {undecimal}, not {undecimal_}.\"\n", " )\n", " assert correct\n", "\n", "\n", "test_decimal_to_undecimal(\"X\", 10)\n", "test_decimal_to_undecimal(\"0\", 0)\n", "test_decimal_to_undecimal(\"1752572309X478\", 57983478668530)" ] }, { "cell_type": "code", "execution_count": 16, "id": "b7069a4b", "metadata": { "code_folding": [ 0 ], "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "e824531a2ae5e3112d2cb45ea8a244d2", "grade": true, "grade_id": "hidden_test-decimal_to_undecimal", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false }, "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "# hidden tests" ] }, { "cell_type": "code", "execution_count": 17, "id": "1563a08f", "metadata": { "code_folding": [ 0 ], "slideshow": { "slide_type": "-" }, "tags": [ "remove-output" ] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "5f6eb9522adb4072b1c1229120003264", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Text(value='10', description='decimal'), Output()), _dom_classes=('widget-interact',))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# undecimal-to-decimal calculator\n", "from ipywidgets import interact\n", "\n", "\n", "@interact(decimal=\"10\")\n", "def convert_decimal_to_undecimal(decimal):\n", " if not decimal.isdigit():\n", " print(\"Not a non-negative integer.\")\n", " else:\n", " print(\"undecimal:\", decimal_to_undecimal(int(decimal)))" ] }, { "cell_type": "markdown", "id": "108d8227", "metadata": {}, "source": [ "## Timer (Optional)" ] }, { "cell_type": "markdown", "id": "f28483d6", "metadata": {}, "source": [ "### Benchmark" ] }, { "cell_type": "markdown", "id": "65f01203", "metadata": {}, "source": [ "Recall the two versions of binary-to-decimal converters defined at the beginning of the lab:" ] }, { "cell_type": "code", "execution_count": 18, "id": "1907679b", "metadata": {}, "outputs": [], "source": [ "binary_to_decimal_v1??" ] }, { "cell_type": "code", "execution_count": 19, "id": "2f817883", "metadata": {}, "outputs": [], "source": [ "binary_to_decimal_v2??" ] }, { "cell_type": "markdown", "id": "b6e0dc3e", "metadata": {}, "source": [ "**How to compare their speed?**" ] }, { "cell_type": "markdown", "id": "aea6bd1a", "metadata": {}, "source": [ "We can use the [time](https://docs.python.org/3/library/time.html) module." ] }, { "cell_type": "code", "execution_count": 20, "id": "749ae181", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "time.struct_time(tm_year=2021, tm_mon=11, tm_mday=19, tm_hour=12, tm_min=9, tm_sec=26, tm_wday=4, tm_yday=323, tm_isdst=0)" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import time\n", "\n", "time.localtime()" ] }, { "cell_type": "markdown", "id": "85279fca", "metadata": {}, "source": [ "We can also [format](https://docs.python.org/3/library/time.html#time.strftime) the current [local time](https://docs.python.org/3/library/time.html#time.localtime) to a more easily readable string:" ] }, { "cell_type": "code", "execution_count": 21, "id": "325a2c68", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'Fri, 19 Nov 2021 12:09:26'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "time.strftime(\"%a, %d %b %Y %H:%M:%S\", time.localtime())" ] }, { "cell_type": "markdown", "id": "d58e4312", "metadata": {}, "source": [ "**How to implement a timer?**" ] }, { "cell_type": "markdown", "id": "b868994c", "metadata": {}, "source": [ "The idea is to record the start time and end time, and then compute their difference:" ] }, { "cell_type": "code", "execution_count": 22, "id": "ebb9c4b5", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for -: 'time.struct_time' and 'time.struct_time'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# code to be timed\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlocaltime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mend\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mstart\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'time.struct_time' and 'time.struct_time'" ] } ], "source": [ "start = time.localtime()\n", "... # code to be timed\n", "end = time.localtime()\n", "end - start" ] }, { "cell_type": "markdown", "id": "d7aa1def", "metadata": {}, "source": [ "Unfortunately, the above code fails because `-` is note defined for the time object `time.struct_time`." ] }, { "cell_type": "markdown", "id": "83c76a51", "metadata": {}, "source": [ "**How to compute the difference in time?**" ] }, { "cell_type": "markdown", "id": "e743b614", "metadata": {}, "source": [ "Instead of implementing `-` for `time.struct_time`, a simpler solution is to use [`time.time()`](https://docs.python.org/3/library/time.html#time.time), which returns the current time as a floating point number." ] }, { "cell_type": "code", "execution_count": 23, "id": "d705ca8c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1637294966.3099682" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "time.time()" ] }, { "cell_type": "markdown", "id": "83d2b6db", "metadata": {}, "source": [ "This is the number of *seconds* elapsed after certain [epoch](https://docs.python.org/3/library/time.html#epoch) (a point in time). For linux/unix, the `time` module uses the [unix epoch](https://en.wikipedia.org/wiki/Unix_time), which is January 1, 1970, 00:00:00 ([UTC](https://en.wikipedia.org/wiki/Coordinated_Universal_Time)):" ] }, { "cell_type": "code", "execution_count": 24, "id": "50924df4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Thu, 01 Jan 1970 00:00:00 +0000 '" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "time.strftime(\n", " \"%a, %d %b %Y %H:%M:%S +0000 \", time.gmtime(0)\n", ") # Convert zero seconds to UTC time" ] }, { "cell_type": "markdown", "id": "0731553c", "metadata": {}, "source": [ "The following code implements the timer:" ] }, { "cell_type": "code", "execution_count": 25, "id": "3a1f3d3d", "metadata": {}, "outputs": [], "source": [ "def time_b2d(binary_to_decimal, binary_str):\n", " \"\"\"Return the time in secs for running binary_to_decimal on binary_str.\"\"\"\n", " start = time.time()\n", " decimal = binary_to_decimal(binary_str)\n", " end = time.time()\n", " return end - start" ] }, { "cell_type": "markdown", "id": "43b9d5ef", "metadata": {}, "source": [ "In the following, `t1` and `t2` keeps the time it takes for `binary_to_decimal_v1` and `binary_to_decimal_v2` to run on the same input byte `binary_str`." ] }, { "cell_type": "code", "execution_count": 26, "id": "d1897ec0", "metadata": { "tags": [ "remove-output" ] }, "outputs": [ { "ename": "StdinNotImplementedError", "evalue": "raw_input was called, but this frontend does not support input requests.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mStdinNotImplementedError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Ready? [Y/n]\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;34m\"n\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mbinary_str\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"1\"\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;36m8\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mt1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime_b2d\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbinary_to_decimal_v1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbinary_str\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mt2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime_b2d\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbinary_to_decimal_v2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbinary_str\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"t1 = {t1:.3g}s\\nt2 = {t2:.3g}s\\nt1/t2 = {t1/t2:.3g}\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/my-conda-envs/jb/lib/python3.8/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 843\u001b[0m \"\"\"\n\u001b[1;32m 844\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_allow_stdin\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 845\u001b[0;31m raise StdinNotImplementedError(\n\u001b[0m\u001b[1;32m 846\u001b[0m \u001b[0;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 847\u001b[0m )\n", "\u001b[0;31mStdinNotImplementedError\u001b[0m: raw_input was called, but this frontend does not support input requests." ] } ], "source": [ "if input(\"Ready? [Y/n]\").lower() != \"n\":\n", " binary_str = \"1\" * 8\n", " t1 = time_b2d(binary_to_decimal_v1, binary_str)\n", " t2 = time_b2d(binary_to_decimal_v2, binary_str)\n", " print(f\"t1 = {t1:.3g}s\\nt2 = {t2:.3g}s\\nt1/t2 = {t1/t2:.3g}\")" ] }, { "cell_type": "markdown", "id": "fd8b0dfd", "metadata": {}, "source": [ "**Exercise** (Optional) Observe the difference in speeds for longer `binary_str`, e.g., with `binary_str = '1' * 100000`. Is the ratio `t1/t2` of the running times roughly the same regardless of the input length?" ] }, { "cell_type": "markdown", "id": "bb0c3e2a", "metadata": {}, "source": [ "There can be variations in the running time due to many factors. To measure the typical performance, we should run the code multiple times and report the total or average running time. The following is the modified timer:" ] }, { "cell_type": "code", "execution_count": 27, "id": "99d7ddf5", "metadata": { "tags": [] }, "outputs": [], "source": [ "def time_b2d(binary_to_decimal, binary_str, n_iters):\n", " \"\"\"Return the time in secs for running binary_to_decimal on binary_str.\"\"\"\n", " start = time.time()\n", " for i in range(n_iters):\n", " decimal = binary_to_decimal(binary_str)\n", " end = time.time()\n", " return end - start" ] }, { "cell_type": "markdown", "id": "815d5721", "metadata": {}, "source": [ "To compare:" ] }, { "cell_type": "code", "execution_count": 28, "id": "b75ac294", "metadata": { "tags": [ "remove-output" ] }, "outputs": [ { "ename": "StdinNotImplementedError", "evalue": "raw_input was called, but this frontend does not support input requests.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mStdinNotImplementedError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Ready? [Y/n]\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;34m\"n\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mbinary_str\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"1\"\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0;36m8\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mn_iters\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mt1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime_b2d\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbinary_to_decimal_v1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbinary_str\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn_iters\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mt2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime_b2d\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbinary_to_decimal_v2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbinary_str\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn_iters\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/my-conda-envs/jb/lib/python3.8/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 843\u001b[0m \"\"\"\n\u001b[1;32m 844\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_allow_stdin\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 845\u001b[0;31m raise StdinNotImplementedError(\n\u001b[0m\u001b[1;32m 846\u001b[0m \u001b[0;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 847\u001b[0m )\n", "\u001b[0;31mStdinNotImplementedError\u001b[0m: raw_input was called, but this frontend does not support input requests." ] } ], "source": [ "if input(\"Ready? [Y/n]\").lower() != \"n\":\n", " binary_str = \"1\" * 8\n", " n_iters = 100\n", " t1 = time_b2d(binary_to_decimal_v1, binary_str, n_iters)\n", " t2 = time_b2d(binary_to_decimal_v2, binary_str, n_iters)\n", " print(f\"t1 = {t1:.3g}s\\nt2 = {t2:.3g}s\\nt1/t2 = {t1/t2:.3g}\")" ] }, { "cell_type": "markdown", "id": "5219d2fa", "metadata": {}, "source": [ "**Exercise** (Optional) Increase the number of iterations until you get can a variation in `t1/t2` less than 0.2 in two consecutive runs most of the time." ] }, { "cell_type": "markdown", "id": "d456ba46", "metadata": {}, "source": [ "Indeed, IPython has a [built-in magic](https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-timeit) to time an execution:" ] }, { "cell_type": "code", "execution_count": 29, "id": "0cfb423c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.52 µs ± 15.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], "source": [ "%%timeit\n", "binary_to_decimal_v1(\"1\" * 8)" ] }, { "cell_type": "code", "execution_count": 30, "id": "151c20ce", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "966 ns ± 4.76 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n" ] } ], "source": [ "%%timeit\n", "binary_to_decimal_v2(\"1\" * 8)" ] }, { "cell_type": "markdown", "id": "e2f7e106", "metadata": {}, "source": [ "Note that the numbers of loops are decided automatically." ] }, { "cell_type": "markdown", "id": "b11e6749", "metadata": {}, "source": [ "### Alarm clock" ] }, { "cell_type": "markdown", "id": "33e3d512", "metadata": {}, "source": [ "**How to set an alarm in python to go off after certain time?**" ] }, { "cell_type": "markdown", "id": "53b5775b", "metadata": {}, "source": [ "We can write a loop like the following:" ] }, { "cell_type": "code", "execution_count": 31, "id": "4b38fba5", "metadata": { "tags": [ "remove-output" ] }, "outputs": [ { "ename": "StdinNotImplementedError", "evalue": "raw_input was called, but this frontend does not support input requests.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mStdinNotImplementedError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mduration\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"How many seconds to wait?\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mstart\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mstart\u001b[0m \u001b[0;34m<=\u001b[0m \u001b[0mduration\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mpass\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Time's up!\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/my-conda-envs/jb/lib/python3.8/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 843\u001b[0m \"\"\"\n\u001b[1;32m 844\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_allow_stdin\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 845\u001b[0;31m raise StdinNotImplementedError(\n\u001b[0m\u001b[1;32m 846\u001b[0m \u001b[0;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 847\u001b[0m )\n", "\u001b[0;31mStdinNotImplementedError\u001b[0m: raw_input was called, but this frontend does not support input requests." ] } ], "source": [ "duration = int(input(\"How many seconds to wait?\"))\n", "start = time.time()\n", "while time.time() - start <= duration:\n", " pass\n", "print(\"Time's up!\")" ] }, { "cell_type": "markdown", "id": "e5c83721", "metadata": {}, "source": [ "A simpler way is to use `time.sleep`:" ] }, { "cell_type": "code", "execution_count": 32, "id": "2432c9fb", "metadata": { "tags": [ "remove-output" ] }, "outputs": [ { "ename": "StdinNotImplementedError", "evalue": "raw_input was called, but this frontend does not support input requests.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mStdinNotImplementedError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"How many seconds to wait?\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun_line_magic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'pinfo'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'time.sleep'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/my-conda-envs/jb/lib/python3.8/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 843\u001b[0m \"\"\"\n\u001b[1;32m 844\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_allow_stdin\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 845\u001b[0;31m raise StdinNotImplementedError(\n\u001b[0m\u001b[1;32m 846\u001b[0m \u001b[0;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 847\u001b[0m )\n", "\u001b[0;31mStdinNotImplementedError\u001b[0m: raw_input was called, but this frontend does not support input requests." ] } ], "source": [ "time.sleep(int(input(\"How many seconds to wait?\")))\n", "time.sleep?" ] }, { "cell_type": "markdown", "id": "e2e1a27c", "metadata": {}, "source": [ "**How to play a sound when time is up?**" ] }, { "cell_type": "markdown", "id": "670f644c", "metadata": {}, "source": [ "To play a beep sound in jupyter notebook, we can use the `jupyter_beeper` module:" ] }, { "cell_type": "code", "execution_count": 33, "id": "ad5025d6", "metadata": { "tags": [ "remove-output" ] }, "outputs": [ { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import jupyter_beeper\n", "\n", "beeper = jupyter_beeper.Beeper()\n", "\n", "\n", "def alarm(seconds):\n", " time.sleep(seconds)\n", " beeper.beep()" ] }, { "cell_type": "markdown", "id": "59cee997", "metadata": {}, "source": [ "To set the alarm to 3 seconds after now:" ] }, { "cell_type": "code", "execution_count": 34, "id": "5d92aa23", "metadata": { "tags": [ "remove-output" ] }, "outputs": [ { "ename": "StdinNotImplementedError", "evalue": "raw_input was called, but this frontend does not support input requests.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mStdinNotImplementedError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0malarm\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"How many seconds to wait?\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m~/my-conda-envs/jb/lib/python3.8/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 843\u001b[0m \"\"\"\n\u001b[1;32m 844\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_allow_stdin\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 845\u001b[0;31m raise StdinNotImplementedError(\n\u001b[0m\u001b[1;32m 846\u001b[0m \u001b[0;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 847\u001b[0m )\n", "\u001b[0;31mStdinNotImplementedError\u001b[0m: raw_input was called, but this frontend does not support input requests." ] } ], "source": [ "alarm(int(input(\"How many seconds to wait?\")))" ] }, { "cell_type": "markdown", "id": "7a0b3fd4", "metadata": {}, "source": [ "Notice that the alarm is blocking the execution. To avoid that, we need to run it as a [thread](https://docs.python.org/3/library/threading.html):" ] }, { "cell_type": "code", "execution_count": 35, "id": "0c0386ad", "metadata": {}, "outputs": [], "source": [ "import threading\n", "\n", "\n", "def background_alarm(seconds):\n", " threading.Thread(target=alarm, args=(seconds,)).start()" ] }, { "cell_type": "markdown", "id": "d48ee605", "metadata": {}, "source": [ "To set two alarms simultaneously:" ] }, { "cell_type": "code", "execution_count": 36, "id": "ebc3186b", "metadata": { "tags": [ "remove-output" ] }, "outputs": [ { "ename": "StdinNotImplementedError", "evalue": "raw_input was called, but this frontend does not support input requests.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mStdinNotImplementedError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mbackground_alarm\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"How many seconds to wait?\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mbackground_alarm\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"How many seconds to wait?\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/my-conda-envs/jb/lib/python3.8/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 843\u001b[0m \"\"\"\n\u001b[1;32m 844\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_allow_stdin\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 845\u001b[0;31m raise StdinNotImplementedError(\n\u001b[0m\u001b[1;32m 846\u001b[0m \u001b[0;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 847\u001b[0m )\n", "\u001b[0;31mStdinNotImplementedError\u001b[0m: raw_input was called, but this frontend does not support input requests." ] } ], "source": [ "background_alarm(int(input(\"How many seconds to wait?\")))\n", "background_alarm(int(input(\"How many seconds to wait?\")))" ] }, { "cell_type": "markdown", "id": "f40fdd7a", "metadata": {}, "source": [ "**Exercise** (Optional) Modify `alarm` to give 3 consecutive beeps in 1 second interval after a duration (in seconds) specified by the user." ] } ], "metadata": { "jupytext": { "text_representation": { "extension": ".md", "format_name": "myst", "format_version": 0.13, "jupytext_version": "1.10.3" } }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" }, "source_map": [ 14, 18, 23, 27, 31, 35, 39, 43, 60, 68, 72, 81, 89, 93, 101, 113, 117, 137, 145, 182, 202, 225, 229, 239, 243, 253, 275, 308, 328, 351, 355, 359, 363, 367, 378, 387, 393, 397, 411, 427, 431, 435, 439, 443, 466, 501, 521, 538, 542, 546, 550, 554, 556, 560, 564, 568, 572, 576, 580, 584, 589, 593, 597, 601, 603, 607, 611, 615, 622, 626, 634, 638, 642, 652, 656, 665, 669, 673, 678, 681, 685, 689, 693, 697, 705, 709, 714, 718, 722, 733, 737, 741, 745, 751, 755, 760 ], "widgets": { "application/vnd.jupyter.widget-state+json": { "state": { "09562043af3f400397ad4810628f0daa": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "TextModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "TextModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "TextView", "continuous_update": true, "description": "decimal", "description_tooltip": null, "disabled": false, "layout": "IPY_MODEL_994c2e33df75412c943aa99d1f53a56d", "placeholder": "​", "style": "IPY_MODEL_af5ad9c692d34f3bb22430ddb69cd7dc", "value": "11" } }, "0961d23cf01849f4932cb317aa1a2026": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "TextModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "TextModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "TextView", "continuous_update": true, "description": "decimal", "description_tooltip": null, "disabled": false, "layout": "IPY_MODEL_57f99ac8830247f4b9248c4456439e63", "placeholder": "​", "style": "IPY_MODEL_6f683ac31d9d46b580e32398162908d4", "value": "10" } }, "0973eade7d624f7c9224ec738e66d3c2": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "188aeee97c75443891238987e74190e2": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "DescriptionStyleModel", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "1aa044f59e65418d940fa9245d6367c8": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "VBoxModel", "state": { "_dom_classes": [ "widget-interact" ], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "VBoxModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "VBoxView", "box_style": "", "children": [ "IPY_MODEL_3ac6135420574ab99e2becca1d591a81", "IPY_MODEL_7c8cdec9605947f0bbca640888d2aca6" ], "layout": "IPY_MODEL_0973eade7d624f7c9224ec738e66d3c2" } }, "1be6939d67264ff6a0fb1e5c33173f09": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "2120981a0ff148b78131a594d7ba4766": { "model_module": "@jupyter-widgets/output", "model_module_version": "1.0.0", "model_name": "OutputModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/output", "_model_module_version": "1.0.0", "_model_name": "OutputModel", "_view_count": null, "_view_module": "@jupyter-widgets/output", "_view_module_version": "1.0.0", "_view_name": "OutputView", "layout": "IPY_MODEL_c632f53d8e25439eaac7df7744554993", "msg_id": "", "outputs": [ { "name": "stdout", "output_type": "stream", "text": "binary: 1011\n" } ] } }, "399f895b70ee46948318a49d7f3e8ea7": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "VBoxModel", "state": { "_dom_classes": [ "widget-interact" ], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "VBoxModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "VBoxView", "box_style": "", "children": [ "IPY_MODEL_09562043af3f400397ad4810628f0daa", "IPY_MODEL_2120981a0ff148b78131a594d7ba4766" ], "layout": "IPY_MODEL_776438ec48744ee1bcf640034946df29" } }, "3ac6135420574ab99e2becca1d591a81": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "TextModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "TextModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "TextView", "continuous_update": true, "description": "undecimal_str", "description_tooltip": null, "disabled": false, "layout": "IPY_MODEL_5081ffd4b251488889cd26816f186a4f", "placeholder": "​", "style": "IPY_MODEL_7d5f034e0a034593b8ffb8337ed5f5f9", "value": "X" } }, "40c220dbc85a4e8fa23905c18e30b855": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "4c682fa3608a44e79914ceeb78a30d4a": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "5081ffd4b251488889cd26816f186a4f": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "57f99ac8830247f4b9248c4456439e63": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "5dabd75d1df24606bac0f779f433686d": { "model_module": "@jupyter-widgets/output", "model_module_version": "1.0.0", "model_name": "OutputModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/output", "_model_module_version": "1.0.0", "_model_name": "OutputModel", "_view_count": null, "_view_module": "@jupyter-widgets/output", "_view_module_version": "1.0.0", "_view_name": "OutputView", "layout": "IPY_MODEL_4c682fa3608a44e79914ceeb78a30d4a", "msg_id": "", "outputs": [ { "ename": "NotImplementedError", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNotImplementedError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m~/my-conda-envs/jb/lib/python3.8/site-packages/ipywidgets/widgets/interaction.py\u001b[0m in \u001b[0;36mupdate\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 254\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mwidget\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_interact_value\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 255\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mwidget\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_kwarg\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 256\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 257\u001b[0m \u001b[0mshow_inline_matplotlib_plots\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 258\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mauto_display\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresult\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mconvert_byte_to_decimal\u001b[0;34m(binary_str)\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"decimal:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbinary_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbinary_str\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mbinary_to_decimal\u001b[0;34m(binary_str)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mbinary_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbinary_str\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# YOUR CODE HERE\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mNotImplementedError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mdecimal\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNotImplementedError\u001b[0m: " ] } ] } }, "5f6eb9522adb4072b1c1229120003264": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "VBoxModel", "state": { "_dom_classes": [ "widget-interact" ], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "VBoxModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "VBoxView", "box_style": "", "children": [ "IPY_MODEL_0961d23cf01849f4932cb317aa1a2026", "IPY_MODEL_bd040bcb22384a23b35d995499c356d4" ], "layout": "IPY_MODEL_40c220dbc85a4e8fa23905c18e30b855" } }, "6f683ac31d9d46b580e32398162908d4": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "DescriptionStyleModel", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "776438ec48744ee1bcf640034946df29": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "7c8cdec9605947f0bbca640888d2aca6": { "model_module": "@jupyter-widgets/output", "model_module_version": "1.0.0", "model_name": "OutputModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/output", "_model_module_version": "1.0.0", "_model_name": "OutputModel", "_view_count": null, "_view_module": "@jupyter-widgets/output", "_view_module_version": "1.0.0", "_view_name": "OutputView", "layout": "IPY_MODEL_1be6939d67264ff6a0fb1e5c33173f09", "msg_id": "", "outputs": [ { "ename": "NotImplementedError", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNotImplementedError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m~/my-conda-envs/jb/lib/python3.8/site-packages/ipywidgets/widgets/interaction.py\u001b[0m in \u001b[0;36mupdate\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 254\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mwidget\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_interact_value\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 255\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mwidget\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_kwarg\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 256\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 257\u001b[0m \u001b[0mshow_inline_matplotlib_plots\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 258\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mauto_display\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresult\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mconvert_undecimal_to_decimal\u001b[0;34m(undecimal_str)\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"decimal:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mundecimal_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mundecimal_str\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mundecimal_to_decimal\u001b[0;34m(undecimal_str)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mundecimal_to_decimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mundecimal_str\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# YOUR CODE HERE\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mNotImplementedError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mdecimal\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNotImplementedError\u001b[0m: " ] } ] } }, "7d5f034e0a034593b8ffb8337ed5f5f9": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "DescriptionStyleModel", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "96396528b029409387bab1fa73cc86bb": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "994c2e33df75412c943aa99d1f53a56d": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "9e2bc734565b4039a2362c0848cee714": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "TextModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "TextModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "TextView", "continuous_update": true, "description": "binary_str", "description_tooltip": null, "disabled": false, "layout": "IPY_MODEL_96396528b029409387bab1fa73cc86bb", "placeholder": "​", "style": "IPY_MODEL_188aeee97c75443891238987e74190e2", "value": "1011" } }, "aefb1e67616b4fc485e083336432f8d0": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "VBoxModel", "state": { "_dom_classes": [ "widget-interact" ], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "VBoxModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "VBoxView", "box_style": "", "children": [ "IPY_MODEL_9e2bc734565b4039a2362c0848cee714", "IPY_MODEL_5dabd75d1df24606bac0f779f433686d" ], "layout": "IPY_MODEL_ebb9c746c83c401dba1cbac3292791fe" } }, "af5ad9c692d34f3bb22430ddb69cd7dc": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "DescriptionStyleModel", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "bd040bcb22384a23b35d995499c356d4": { "model_module": "@jupyter-widgets/output", "model_module_version": "1.0.0", "model_name": "OutputModel", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/output", "_model_module_version": "1.0.0", "_model_name": "OutputModel", "_view_count": null, "_view_module": "@jupyter-widgets/output", "_view_module_version": "1.0.0", "_view_name": "OutputView", "layout": "IPY_MODEL_e5c25fd51efd4cc3b36c7d056d33933d", "msg_id": "", "outputs": [ { "ename": "NotImplementedError", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNotImplementedError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m~/my-conda-envs/jb/lib/python3.8/site-packages/ipywidgets/widgets/interaction.py\u001b[0m in \u001b[0;36mupdate\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 254\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mwidget\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_interact_value\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 255\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mwidget\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_kwarg\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 256\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 257\u001b[0m \u001b[0mshow_inline_matplotlib_plots\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 258\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mauto_display\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresult\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mconvert_decimal_to_undecimal\u001b[0;34m(decimal)\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Not a non-negative integer.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"undecimal:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdecimal_to_undecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdecimal\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m\u001b[0m in \u001b[0;36mdecimal_to_undecimal\u001b[0;34m(decimal)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mdecimal_to_undecimal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdecimal\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# YOUR CODE HERE\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mNotImplementedError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mundecimal_str\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNotImplementedError\u001b[0m: " ] } ] } }, "c632f53d8e25439eaac7df7744554993": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "e5c25fd51efd4cc3b36c7d056d33933d": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "ebb9c746c83c401dba1cbac3292791fe": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }